package advancedpart;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader infile = null;

        try {
            infile = new BufferedReader(new FileReader(args[0]));
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
            return;
        }

        DynamicHash dh = new DynamicHash();
        String word = infile.readLine();
        String lastURL = "";
		
        while (word != null) {
            if (word.length() >= 6 && word.substring(0, 6).equals("*PAGE:"))
                lastURL = word.substring(6);
            else
                dh.put(word, lastURL);

            word = infile.readLine();
        }

        BufferedReader inuser = new BufferedReader(new InputStreamReader(System.in));
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        boolean quit = false;
		
        while (!quit)  {
            System.out.print("Search for: ");
            String searchWord = inuser.readLine();

            if (searchWord.isEmpty())
                quit = true;
            else engine:{
                String urls = "";

                if (searchWord.contains(" AND ")) {
                    String[] searchCommand = searchWord.split(" AND ");
                    String urls1 = dh.getURLs(searchCommand[0]);
                    String urls2 = "\n" + dh.getURLs(searchCommand[1]);
                    
                    if (urls1.isEmpty() || urls2.isEmpty()) {
                    	System.out.println("No URLs found containing the words \"" + searchCommand[0] + "\" AND \""+searchCommand[1]+"\"\n");
                        break engine;
                    } else {
                        String[] urls1Array = urls1.split("\n");
                        String[] urls2Array = urls2.split("\n");

						//First method
                        for (String s : urls1Array) {
                            if (map.containsKey(s))
                                map.put(s, map.get(s) + 1);
                            else
                                map.put(s, 1);
                        }

                        for (String s : urls2Array) {
                            if (map.containsKey(s))
                                map.put(s, map.get(s) + 1);
                            else
                                map.put(s, 1);
                        }

                        if (!map.keySet().isEmpty()) {
                            for (String key : map.keySet()) {
                                if (map.get(key) > 1)
                                    urls += key + "\n";
                            }
                        }

                        map.clear();
						
						//Second method
						// for (String s : urls1Array) {
						   // for (int i = 0; i < urls2Array.length; ++i) {
							   // if (s.equals(urls2Array[i]))
								   // urls += urls2Array[i] + "\n";
						   // }
						// }

						// for (Object obj : map.keySet())
						// System.out.println((String)obj);

						// map.clear();
                    }
                } else if (searchWord.contains(" OR ")) {
                    String[] searchCommand = searchWord.split(" OR ");
                    urls += dh.getURLs(searchCommand[0]);
                    urls += "\n" + dh.getURLs(searchCommand[1]);
                    if (urls.equals("\n")){
                    	System.out.println("No URLs found containing the words \"" + searchCommand[0] + "\" OR \""+searchCommand[1]+"\"\n");
                        break engine;
                    }

                    for (String s : urls.split("\n"))
                        map.put(s, 1);

                    Object[] temp = map.keySet().toArray();
                    urls = "";

                    for (Object o : temp)
                        urls += (String)o + "\n";

                    map.clear();
                } else
                    urls = dh.getURLs(searchWord);

                if (urls.isEmpty()) {
                	System.out.println("No URLs found for word(s) \"" + searchWord + "\"\n");
                } else
                    System.out.println(urls + "\n");
            }
        }

        //System.out.println("Resizes: " + dh.noResized());
    }
}